home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / utilities / cdrom / amiget.lha / AmiGet.c next >
Encoding:
C/C++ Source or Header  |  1994-05-10  |  4.6 KB  |  233 lines

  1. /*
  2.  * AmiGet.c - very quick hack to create amigaguide docs from search output
  3.  * Urban Mueller, umueller@wuarchive.wustl.edu, umueller@amiga.icu.net.ch
  4.  */
  5.  
  6. #define INDEXNAME   "AMINET_0294:INDEX"
  7. #define INDEXLEN    700000
  8. #define AMINETFILES "AMINET_0294:Aminet/"
  9. #define MORE        "sys:utilities/more"
  10.  
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <stdlib.h>
  14. #include <exec/types.h>
  15. #include <dos/dostags.h>
  16. #include <libraries/dos.h>
  17.  
  18. char *path = AMINETFILES;
  19. char *index= INDEXNAME;
  20.  
  21. char name[200], dir[200], fill[200], readme[200], sys[200], str[200];
  22. char low[256], head[1000], *hp=head, tempnam[200];
  23.  
  24. FILE *g;
  25. BPTR f;
  26.  
  27. char *linebeg (char *str)
  28. {
  29.   while (*--str != '\n')
  30.     ;
  31.   return str+1;
  32. }
  33.  
  34. char *stristr(str1, str2)
  35.   char *str1, *str2;
  36. {
  37.   char c = low[(unsigned char)*str1], l = strlen(str1);
  38.  
  39.   for (;;) {
  40.     while (low[(unsigned char)*str2++] != c)
  41.       ;
  42.     str2--;
  43.  
  44.     if (!strnicmp(str1, str2, l))
  45.       return str2;
  46.  
  47.     str2++;
  48.   }
  49.  
  50.   return 0;
  51. }
  52.  
  53. char dnbuf[50];
  54.  
  55. char *
  56. dirhead(str)
  57.   char *str;
  58. {
  59.   char *t=dnbuf;
  60.  
  61.   while (*str && *str!='/')
  62.     *t++ = *str++;
  63.   *t=0;
  64.  
  65.   return dnbuf;
  66. }
  67.  
  68. mysystem( char *nam )
  69. {
  70.   char buf[200];
  71.   long *arr={TAG_DONE};
  72.  
  73.   sprintf (buf, "%s %s", nam, tempnam);
  74.  
  75.   return System (buf, arr);
  76. }
  77.  
  78. main(int argc, char *argv[])
  79. {
  80.   char buf[200], cdir[200], fdir[200], *s, *d;
  81.   char *mem, *pos, *end, *desc;
  82.   int  mlen, nlen, find=0, i, n=0;
  83.  
  84.   sprintf (tempnam,"t:netguide.%x",buf);
  85.   *cdir=*fdir=0;
  86.  
  87.   if (!(mem=malloc(INDEXLEN+400)))
  88.     printf("Out of memory, try adapting INDEXLEN\n"), exit(20);
  89.   
  90.   if (!(f=Open(index,MODE_OLDFILE)))
  91.     printf("Cannot open Aminet %s for input\n",index), exit(20);
  92.  
  93.   mlen=Read(f,mem+16,INDEXLEN);
  94.   Close(f);
  95.  
  96.   mem[15]='\n';
  97.   mem+=16;
  98.  
  99.   if (!(g=fopen(tempnam,"w")))
  100.     printf("Cannot open %s for output\n",tempnam), exit(20);
  101.  
  102.   for (i=0; i<256; i++)
  103.     low[i]= i>='A' && i<='Z' ? i-'A'+'a' : i;
  104.  
  105.   if( argc>1)
  106.     strcpy (str, argv[1]), find=1;
  107.  
  108.  
  109.   if (argc==0) {
  110.     printf("Enter string to look for:\n");
  111.     printf("(or hit RETURN to see full index)\n> ");
  112.     fflush(stdout);
  113.     gets(str);
  114.     find= *str;
  115.     printf("Processing...\n");
  116.     fflush(stdout);
  117.   }
  118.  
  119.  
  120.   if (find) {
  121.  
  122.     fprintf (g,"@database Aminet\n@node main \"Aminet files matching '%s'\"\n", str);
  123.     fprintf (g, "Click name to unpack to RAM:, or click description to read .readme\n\n");
  124.     strlwr(str);
  125.     strcpy  ( mem+mlen, str);
  126.  
  127.   } else {
  128.  
  129.     hp+= sprintf (hp,"@database Aminet\n@node main \"Complete Aminet index\"\n");
  130.     hp+= sprintf (hp,"Please pick a directory\n\n");
  131.     for (i=0; i<30; i++)
  132.       fprintf (g, "                               \n");
  133.  
  134.   }
  135.  
  136.   end= mem+mlen;
  137.   pos= find ? linebeg ( stristr(str, mem) ) : mem;
  138.  
  139.   while (pos<end) {
  140.  
  141.     if (*pos=='|') {
  142.       pos= strchr(pos,'\n')+1;
  143.       continue;
  144.     }
  145.  
  146.     /*--------------------- formatting ---------------- */
  147.  
  148.     for ( s=pos, d=name; *s && *s!=' '; )
  149.       *d++ = *s++;
  150.     *d= 0;
  151.     nlen=s-pos;
  152.  
  153.     for ( s=pos+nlen, d=fill; s<pos+37; )
  154.       *d++ = *s++;
  155.     *d= 0;
  156.  
  157.     for ( s=pos+21, d=dir; *s!=' '; )
  158.       *d++ = *s++;
  159.     *d= 0;
  160.  
  161.     for ( s=pos+21; *s!='\n'; s++ )
  162.       if (*s=='\"')
  163.         *s='\'';
  164.     *s=0;
  165.  
  166.     strcpy(readme, name);
  167.     if (nlen>3)
  168.       strcpy(readme+nlen-3,"readme");
  169.  
  170.     desc= pos+37;
  171.  
  172.     /*--------------------- titles -------------------- */
  173.     if (!find) {
  174.       if (strcmp(cdir,dirhead(dir))) {
  175.         strcpy(cdir,dirhead(dir));
  176.         strcpy(fdir, dir);
  177.         hp += sprintf(hp,"  @{\" %-4.4s \" link \"%s\"}\n", cdir, cdir);
  178.  
  179.         fprintf (g,"@endnode\n\n");
  180.         fprintf (g,"@node %s \"Directory %s\"\n", cdir, cdir);
  181.         fprintf (g, "Click name to unpack to RAM:, or click description to read .readme\n\n");
  182.  
  183.       } else if (strcmp (fdir, dir)) {
  184.  
  185.         strcpy  (fdir, dir);
  186.         fprintf (g, "\n"); 
  187.  
  188.       }
  189.     }
  190.  
  191.     fprintf(g,"@{\"%s\" system \"lha e %s%s/%s RAM:\"}%s@{\"%s\" system \"%s %s%s/%s\"}\n",
  192.             name, path, dir, name, fill, desc, MORE, path, dir, readme);
  193.  
  194.     *s++='\n';
  195.     if (find)
  196.       pos= linebeg( stristr ( str, s));
  197.     else
  198.       pos= s;
  199.  
  200.     n++;
  201.   }
  202.  
  203.   if (find) {
  204.  
  205.     fprintf (g, "\n%d files listed\n", n);
  206.     fprintf (g, "@endnode\n");
  207.  
  208.   } else {
  209.  
  210.     fprintf (g, "@endnode\n");
  211.     fseek   (g, 0, 0);
  212.     fprintf (g, head);
  213.  
  214.   }
  215.  
  216.   free(mem-16);
  217.   fclose(g);
  218.  
  219.   if (n || argc==0) {
  220.  
  221.     if( mysystem("sys:utilities/multiview"))
  222.       if( mysystem("sys:utilities/amigaguide"))
  223.         if( mysystem("multiview"))
  224.           if( mysystem("amigaguide"))
  225.             printf("Could not start AmigaGuide\n"), Delay(120), exit(20);
  226.  
  227.   } else 
  228.     printf("No matching files found\n");
  229.       
  230.   unlink(tempnam);
  231.  
  232. }
  233.